package Stages

import Listeners.TouchState
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.InputListener
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.Window
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import floatHeight
import floatWidth
import generateDataUnRepeat
import image
import setOnTouchListener
import stageFlag

/**
 * 商店舞台
 */
class StoreStage(textureAtlas: TextureAtlas) : Stage()
{
    val bgRegion by lazy { textureAtlas.findRegion("shujia") }
    val heartRegion by lazy { textureAtlas.findRegion("heart") }
    val glodRegion by lazy { textureAtlas.findRegion("golden") }
    val moguRegion by lazy { textureAtlas.findRegion("mogu") }


    val bgImage by lazy { Image(bgRegion) }
    val heartImage by lazy { Image(heartRegion) }
    val glodImage by lazy { Image(glodRegion) }
    val moguImage by lazy { Image(moguRegion) }
    val closeTexture by lazy { textureAtlas.findRegion("exit") }
    val exitImage by lazy { Image(closeTexture) }

    val fontGenertor by lazy { FreeTypeFontGenerator(Gdx.files.internal("hksn.ttf")) }
    val fontData by lazy { fontGenertor.generateDataUnRepeat(20, "你认我做大哥，我教你梳中分-马里奥，购买成功") }
    val bitmapFont by lazy { BitmapFont(fontData, fontData.textureRegions, false).apply { color = Color.WHITE } }
    val windowBg by lazy { TextureRegionDrawable(textureAtlas.findRegion("success_bg")) }

    val windowClose by lazy { Image(closeTexture) }
    val windowStyle by lazy {
        Window.WindowStyle(bitmapFont, bitmapFont.color, windowBg)
    }
    val window by lazy {
        Window("购买成功", windowStyle).apply {
            val win = this
            width = Gdx.graphics.floatWidth / 2
            height = Gdx.graphics.floatHeight / 2
            setPosition((Gdx.graphics.floatWidth / 2) - (width / 2), (Gdx.graphics.floatHeight / 2) - (height / 2))
            setModal(true)
            windowClose.setPosition(width - windowClose.width, height - windowClose.height)
            windowClose.setOnTouchListener { _, touchState ->
                if (touchState == TouchState.touchUp)
                {
                    win.remove()
                }
            }
            addActor(windowClose)
        }
    }

    init
    {

        bgImage.setSize(Gdx.graphics.floatWidth, Gdx.graphics.floatHeight)
        moguImage.translate(63f, 194f)
        glodImage.translate(193f, 189f)
        heartImage.translate(325f, 193f)
        exitImage.translate(425f, 266f)

        exitImage.setOnTouchListener { image, touchState ->
            if (touchState == TouchState.touchUp)
            {
                stageFlag = MainPage.STAGE_FLAG.GAME
            }
        }



        heartImage.setOnTouchListener { image, touchState ->
            if (touchState == TouchState.touchUp)
            {
                addActor(window)
            }
        }
        moguImage.setOnTouchListener { image, touchState ->
            if (touchState == TouchState.touchUp)
            {
                addActor(window)
            }
        }
        glodImage.setOnTouchListener { image, touchState ->
            if (touchState == TouchState.touchUp)
            {
                addActor(window)
            }
        }


        addActor(bgImage)
        addActor(glodImage)
        addActor(heartImage)
        addActor(moguImage)
        addActor(exitImage)


    }
}